package com.scitc.pleasurevideo.base;


import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.content.res.TypedArray;
import android.os.Build;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;

import com.githang.statusbar.StatusBarCompat;
import com.scitc.pleasurevideo.R;
import com.umeng.analytics.MobclickAgent;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Map;


@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public abstract class ETNoBackFragmentActivity extends AppCompatActivity {
    public void loginBack(View view) {
        this.finish();
    }

    public static Context mContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        //StatGraph.doPageStart(this);
        StatusBarCompat.setStatusBarColor(this, getResources().getColor(R.color.white), true);
        if (Build.VERSION.SDK_INT == Build.VERSION_CODES.O && isTranslucentOrFloating()) {
            boolean result = fixOrientation();
        }
    }

    private boolean isTranslucentOrFloating() {
        boolean isTranslucentOrFloating = false;
        try {
            int[] styleableRes = (int[]) Class.forName("com.android.internal.R$styleable").getField("Window").get(null);
            final TypedArray ta = obtainStyledAttributes(styleableRes);
            Method m = ActivityInfo.class.getMethod("isTranslucentOrFloating", TypedArray.class);
            m.setAccessible(true);
            isTranslucentOrFloating = (boolean) m.invoke(null, ta);
            m.setAccessible(false);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return isTranslucentOrFloating;
    }


    @Override
    public void setRequestedOrientation(int requestedOrientation) {
        if (Build.VERSION.SDK_INT == Build.VERSION_CODES.O && isTranslucentOrFloating()) {
            return;
        }
        super.setRequestedOrientation(requestedOrientation);
    }

    private boolean fixOrientation() {
        try {
            Field field = Activity.class.getDeclaredField("mActivityInfo");
            field.setAccessible(true);
            ActivityInfo o = (ActivityInfo) field.get(this);
            o.screenOrientation = -1;
            field.setAccessible(false);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        //StatGraph.doPageRestart(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        //StatGraph.doPageResume(this.getClass().getSimpleName(), this);
        try {
            // MobclickAgent.onPageStart(getIdentity());
            MobclickAgent.onResume(this);
        } catch (Exception e) {
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        //StatGraph.doPagePause(this);
        try {
            // MobclickAgent.onPageEnd(getIdentity());
            MobclickAgent.onPause(this);
        } catch (Exception e) {
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        //StatGraph.doPageStop(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //StatGraph.doPageEnd(this);
    }

    @Override
    public void finish() {
        super.finish();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:
                this.finish();
                return true;

        }
        return false;
    }

    public boolean check(Map<String, Object> map) {
        Object code = map.get("code");
        if (code != null) {
            int codeInt = (Integer) code;
            String msg = (String) map.get("msg");
            if (codeInt != 200) {
                if (codeInt == 100) {
                    // toLogin();
                }
                return false;
            }
        }

        return true;
    }

    public String getIdentity() {
        return this.getClass().getName();
    }
}
